package cn.zhixiangsingle.controller.gasSwitchAlert;

import cn.zhixiangsingle.date.DateUtils;
import cn.zhixiangsingle.entity.base.IsEmptyUtils;
import cn.zhixiangsingle.entity.base.site.vo.SiteVO;
import cn.zhixiangsingle.entity.base.user.User;
import cn.zhixiangsingle.entity.gasSwitch.gasSwitchAlert.dto.GasSwitchAlertDTO;
import cn.zhixiangsingle.entity.gasSwitch.gasSwitchAlert.vo.GasSwitchAlertVO;
import cn.zhixiangsingle.excel.ExcelUtil;
import cn.zhixiangsingle.excel.ExcelWriterFactroy;
import cn.zhixiangsingle.service.gasSwitchAlert.GasSwitchAlertService;
import cn.zhixiangsingle.service.site.SiteService;
import cn.zhixiangsingle.web.responsive.IStatusMessage;
import cn.zhixiangsingle.web.responsive.ResultBean;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hhp
 * @description 煤气开关使用view层
 * @date 2019/11/8 12:07
 * @Copyright: 2019 www.zhixiangyun.net Inc. All rights reserved.
 * 注意：本内容仅限于浙江智飨科技内部传阅，禁止外泄以及用于其他的商业目
 */
@Controller
@RequestMapping("/gasSwitchAlert")
public class GasSwitchAlertController {
    private static final Logger logger = LoggerFactory
            .getLogger(GasSwitchAlertController.class);
    @Reference(version = "1.0.0")
    private SiteService siteService;
    @Reference(version = "1.0.0")
    private GasSwitchAlertService gasSwitchAlertService;
    /**
     *@描述
     *@参数 []
     *@返回值 org.springframework.web.servlet.ModelAndView
     *@创建人 hhp
     *@创建时间 2019/11/8
     *@修改人和其它信息
     */
    @RequestMapping(value = "/getListPage", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView getListPage() {
        logger.debug("跳转煤气开关使用信息列表！");
        ModelAndView mav = new ModelAndView("/hjjc/mqkgsy/mqkgsyjl");
        try {
            mav.addObject("msg", "ok");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("跳转煤气开关使用列表异常！", e);
        }
        return mav;
    }
    /**
     *@描述
     *@参数 [gasSwitchAlertDTO]
     *@返回值 cn.zhixiangsingle.web.responsive.ResultBean
     *@创建人 hhp
     *@创建时间 2019/11/8
     *@修改人和其它信息
     */
    @PostMapping(value = "/getGasSwitchAlertList")
    @ResponseBody
    public ResultBean getLibraryList(GasSwitchAlertDTO gasSwitchAlertDTO) {
        logger.debug("获取煤气开关使用信息列表！ gasSwitchAlertDTO = "+gasSwitchAlertDTO);
        ResultBean resultBean = new ResultBean();
        try {
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            gasSwitchAlertDTO.setUserId(existUser.getId());
            resultBean = gasSwitchAlertService.findGasSwitchAlertList(gasSwitchAlertDTO);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取煤气开关使用信息异常！", e);
            resultBean.setSuccess(false);
            resultBean.setResultCode(IStatusMessage.SystemStatus.ERROR.getCode());
            resultBean.setMsg(IStatusMessage.SystemStatus.ERROR.getMessage());
        }
        return resultBean;
    }
    /**
     *@描述
     *@参数 [gasSwitchAlertDTO]
     *@返回值 cn.zhixiangsingle.web.responsive.ResultBean
     *@创建人 hhp
     *@创建时间 2019/11/9
     *@修改人和其它信息
     */
    @RequestMapping(value = "/setGasSwitchAlert", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean setGasSwitchAlert(GasSwitchAlertDTO gasSwitchAlertDTO) {
        logger.debug("添加或修改煤气开关使用信息 --gasSwitchAlertDTO-" + gasSwitchAlertDTO);
        ResultBean resultBean = new ResultBean();
        try {
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            if (!IsEmptyUtils.isEmpty(gasSwitchAlertDTO)) {
                gasSwitchAlertDTO.setUserId(existUser.getId());
                if(!IsEmptyUtils.isEmpty(gasSwitchAlertDTO.getId())){
                    resultBean = gasSwitchAlertService.updateGasSwitchAlert(gasSwitchAlertDTO);
                }else{
                    resultBean = siteService.getUserSites(existUser.getId());
                    if(!IsEmptyUtils.isEmpty(resultBean.getResult())){
                        List<SiteVO> siteVOList = (List<SiteVO>)resultBean.getResult();
                        if(siteVOList.size()>1){
                            if(!IsEmptyUtils.isEmpty(gasSwitchAlertDTO.getSdIds())){
                                resultBean = gasSwitchAlertService.addGasSwitchAlert(gasSwitchAlertDTO);
                            }else{
                                resultBean.setSuccess(false);
                                resultBean.setResultCode(IStatusMessage.SystemStatus.SITE_ID_LOSE.getCode());
                                resultBean.setMsg(IStatusMessage.SystemStatus.SITE_ID_LOSE.getMessage());
                            }
                        }else{
                            gasSwitchAlertDTO.setSdId(existUser.getSdId());
                            gasSwitchAlertDTO.setSdIds("");
                            resultBean = gasSwitchAlertService.addGasSwitchAlert(gasSwitchAlertDTO);
                        }
                    }else{
                        resultBean.setSuccess(false);
                        resultBean.setResultCode(IStatusMessage.SystemStatus.SITE_ROLE_IS_EMPTY.getCode());
                        resultBean.setMsg(IStatusMessage.SystemStatus.SITE_ROLE_IS_EMPTY.getMessage());
                    }
                }
            }else{
                resultBean.setSuccess(false);
                resultBean.setResultCode(IStatusMessage.SystemStatus.PARAMETERS_LOSE.getCode());
                resultBean.setMsg(IStatusMessage.SystemStatus.PARAMETERS_LOSE.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加或修改煤气开关使用信息异常！", e);
            resultBean.setSuccess(false);
            resultBean.setResultCode(IStatusMessage.SystemStatus.ERROR.getCode());
            resultBean.setMsg(IStatusMessage.SystemStatus.ERROR.getMessage());
        }
        return resultBean;
    }
    /**
     *@描述
     *@参数 [gasSwitchAlertDTO]
     *@返回值 cn.zhixiangsingle.web.responsive.ResultBean
     *@创建人 hhp
     *@创建时间 2019/11/9
     *@修改人和其它信息
     */
    @PostMapping(value = "/delGasSwitchAlert")
    @ResponseBody
    public ResultBean delGasSwitchAlert(GasSwitchAlertDTO gasSwitchAlertDTO){
        logger.debug("删除煤气开关使用信息 --gasSwitchAlertDTO-" + gasSwitchAlertDTO);
        ResultBean resultBean = new ResultBean();
        try {
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            gasSwitchAlertDTO.setUserId(existUser.getId());
            if (!IsEmptyUtils.isEmpty(gasSwitchAlertDTO)) {
                /*if(IsEmptyUtils.isEmpty(ingredientBaseDTO.getSdId())){
                    ingredientBaseDTO
                }*/
                resultBean = gasSwitchAlertService.delGasSwitchAlert(gasSwitchAlertDTO);
            }else{
                resultBean.setSuccess(false);
                resultBean.setResultCode(IStatusMessage.SystemStatus.PARAMETERS_LOSE.getCode());
                resultBean.setMsg(IStatusMessage.SystemStatus.PARAMETERS_LOSE.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除煤气开关使用异常！", e);
            resultBean.setSuccess(false);
            resultBean.setResultCode(IStatusMessage.SystemStatus.ERROR.getCode());
            resultBean.setMsg(IStatusMessage.SystemStatus.ERROR.getMessage());
        }
        return resultBean;
    }
    /**
     *@描述
     *@参数 [response, gasSwitchAlertDTO]
     *@返回值 void
     *@创建人 hhp
     *@创建时间 2019/11/8
     *@修改人和其它信息
     */
    @RequestMapping(value = "/writeGasSwitchAlert", method = RequestMethod.GET)
    public void writeGasSwitchAlerts(HttpServletResponse response, GasSwitchAlertDTO gasSwitchAlertDTO) {
        logger.debug("导出煤气开关使用信息Excel！条件:" + gasSwitchAlertDTO);
        try {
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            gasSwitchAlertDTO.setUserId(existUser.getId());
            if (existUser.getZx() == true || existUser.getSdId() != 22) {
                gasSwitchAlertDTO.setSdId(existUser.getSdId());
            }
            List<ResultBean> ingredientBasesModel = gasSwitchAlertService.getGasSwitchAlerts(gasSwitchAlertDTO);
            ExcelWriterFactroy writer = new ExcelWriterFactroy(ExcelUtil.getOutputStream(DateUtils.formatDate(new Date(), "yyyy-MM-dd-HH-mm:ss-煤气开关使用信息"), response), ExcelTypeEnum.XLSX);
            GasSwitchAlertVO ingredientBaseVO = new GasSwitchAlertVO();
            if (!IsEmptyUtils.isEmpty(ingredientBasesModel)) {
                for (int i = 0; i < ingredientBasesModel.size(); i++) {
                    //sheetNo 不能从0开始
                    Sheet sheet = new Sheet(i + 1, 2, ingredientBaseVO.getClass());
                    sheet.setSheetName(ingredientBasesModel.get(i).getMsg());
                    sheet.setColumnWidthMap((Map) ingredientBasesModel.get(i).getObj());
                    Table table1 = new Table(1);
                    table1.setClazz(GasSwitchAlertVO.class);
                    table1.setHead((List<List<String>>) ingredientBasesModel.get(i).getRows());
                    List<GasSwitchAlertVO> ingredientBaseVOS = (List<GasSwitchAlertVO>) ingredientBasesModel.get(i).getResult();
                    if (!IsEmptyUtils.isEmpty(ingredientBaseVOS)) {
                        writer.write(ingredientBaseVOS, sheet, table1);
                    }
                }
            }
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出煤气开关使用Excel异常！", e);
        }
    }
}
